// app.js
import event from './common/js/event.js'
import {serverUrl,serverUrlgdb} from './common/js/config'
App({
  
onShow(){
    // 获取系统信息
const systemInfo = wx.getSystemInfoSync();
const deviceInfo = wx.getDeviceInfo()
wx.setStorageSync('brand', deviceInfo.brand)
// 获取设备机型
const model = systemInfo.model;
// 判断机型
if (model.includes('iPhone')) {
  wx.setStorageSync('modelid', 'iPhone')
} else if (model.includes('iPad')) {
  wx.setStorageSync('modelid', 'iPad')
} else if (model.includes('Android')) {
  wx.setStorageSync('modelid', 'Android')
} else {
  wx.setStorageSync('modelid', model)
}
    let updateManager = wx.getUpdateManager();
    if (!updateManager) {
     return
    } else {
     //新版本更新
     if (wx.canIUse('getUpdateManager')) {
      //判断当前微信版本是否支持版本更新
      updateManager.onCheckForUpdate(function(res) {
       if (res.hasUpdate) {
        // 请求完新版本信息的回调
        updateManager.onUpdateReady(function() {
         wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function(res) {
           if (res.confirm) {
            updateManager.applyUpdate();
           }
          },
         });
        });
        updateManager.onUpdateFailed(function() {
         wx.showModal({
          // 新的版本下载失败
          title: '已经有新版本了哟~',
          content: '新版本已经上线啦~，请您删除当前小程序，重新搜索(医好加+)打开哟~',
         });
        });
       } else {}
      });
     } else {
      wx.showModal({
       // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
       title: '提示',
       content: '当前微信版本过低，部分功能无法使用，请升级到最新微信版本后重试。',
      });
     }
    }
  },
  onLaunch() {
    wx.onNetworkStatusChange(function (res) {
      if (!res.isConnected) {
        wx.showLoading({
          title: '网络故障',
          mask:true
        })
        // 在这里执行网络断开时需要的逻辑，比如提示用户、停止定时器等
      }else{
        wx.hideLoading()
      }
    });
  },
  showMsg: function (msg, icon) {
    wx.showToast({
      title: msg,
      icon: icon || 'none',
      duration: 2000
    })
  },
  on (name, self, callback) {
    event.on(name, self, callback)
  },
  emit (name, data) {
    event.emit(name, data)
  },
   getUserInfo:function(e){
    let that = this
    let indexTab = wx.getStorageSync('tabNav')
    if(indexTab==0){
      wx.removeStorage('token')
        return wx.reLaunch({
          url: '/pages/login/login',
        })
    }else{
      return new Promise(function(resolve,reject){
        setTimeout(() => {
          wx.request({
            url: (indexTab==3?serverUrlgdb:serverUrl)+(indexTab==0?'/customer/userInfo':'/medical/staffInfo'),
            method:'GET',
            header:{
              'token':wx.getStorageSync('token')
            },
            success:(res=>{
              let resData= res.data
              if(resData.code==200){
                // if(wx.getStorageSync('token')){
                  indexTab==3?wx.setStorageSync('token3', wx.getStorageSync('token')):wx.setStorageSync('token1', wx.getStorageSync('token'))
                // }
                // wx.setStorageSync('token', wx.getStorageSync('token')?wx.getStorageSync('token'):(indexTab==3?wx.getStorageSync('token3'):wx.getStorageSync('token1')))
                if(indexTab==1 || indexTab==3){
                  that.globalData.userInfo = resData.data.staffInfo
                  wx.setStorageSync('staffInfoIstype', resData.data.staffInfo.hospital.operationProject)
                  resolve(resData.data.staffInfo)
                }else{
                  that.globalData.userInfo = resData.data.customerInfo
                  resolve(resData.data.customerInfo)
                }
              }else{
                resolve(resData)
              }
            })
          })
        });
      })
    }
    
  },
  getHost:function(e){
    let that = this
    let indexTab = wx.getStorageSync('tabNav')
    return new Promise(function(resolve,reject){
      setTimeout(() => {
        wx.request({
          url: (indexTab==3?serverUrlgdb:serverUrl)+'/hospital/detail/'+e,
          method:'GET',
          header:{
            'token':wx.getStorageSync('token')
          },
          success:(res=>{
            let resData= res.data
            if(resData.code==200){
              resolve(resData.data.hospital)
            }else{
              reject(resData.data.message)
            }
          })
        })
      });
    })
  },
  loginCode:function(){
    return new Promise(function(resolve,reject){
      wx.login({
        success:(res=>{
          resolve(res.code)
        })
      })
    })
  },
  globalData: {
    userInfo: null,
    serverUrl:serverUrl,
    serverUrlgdb:serverUrlgdb,
    isNumLock:3000,
    search: '',
    key:'ZLGBZ-2TNHC-FU32Q-AXKZY-JY4WJ-DIF2I',
    colorMap: {
      "待处理": "#FF703E",
      "进行中": "#0584FE",
      "未开始": "#0584FE",
      "已逾期": "#FE294D",
      "已完成": "#989898",
      "已拒绝": "#FE294D",
      "转诊中": "#0584FE"
    },
    colorMaps: {
      "0": "#FF703E",
      "1": "#0584FE",
      "3": "#FE294D",
      "2": "#989898",
    }
  }
})
